package com.yq.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yq.reggie.pojo.Category;
import com.yq.reggie.service.CategoryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/category")
@Slf4j
public class CategoryController {
    @Autowired
    private CategoryService categoryService;

    /**
     * 添加类别：添加菜品分类，添加套餐分类
     * @param category
     * @return
     */
    @PostMapping
    public Result addCategory(@RequestBody Category category){
         categoryService.save(category);
         log.info("类别添加：{}",category);
        return Result.success("");
    }

    /**
     * 分页展示
     * @param page
     * @param pageSize
     * @return
     */
    @GetMapping("/page")
    public Result selectPage(Integer page,Integer pageSize){
        //分页查询
        Page<Category> pageInfo = new Page<>(page, pageSize);
        LambdaQueryWrapper<Category> wrapper = new LambdaQueryWrapper<>();
        wrapper.orderByAsc(Category::getCreateTime);
        categoryService.page(pageInfo,wrapper);
        log.info("分页查询：{}",pageInfo);
        return Result.success(pageInfo);
    }

    /**
     * 类别修改
     * @param category
     * @return
     */
    @PutMapping
    public Result modify(@RequestBody Category category){
        categoryService.updateById(category);
        log.info("类别修改：{}",category.getId());
        return Result.success("");
    }

    /**
     * 删除类别
     * @param ids
     * @return
     */
    @DeleteMapping
    public Result delete(Long ids){
       return categoryService.remove(ids);
    }

    /**
     * 根据type查询类别
     * @param category
     * @return
     */
    @GetMapping("/list")
    public Result list(Category category){
        LambdaQueryWrapper<Category> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(category.getType()!=null,Category::getType,category.getType());
        List<Category> list = categoryService.list(queryWrapper);
        return Result.success(list);
    }

}
